home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / gs3.53 / pdf_2ps.ps < prev    next >
Text File  |  1996-01-10  |  6KB  |  207 lines

  1. %    Copyright (C) 1994, 1995 Aladdin Enterprises.  All rights reserved.
  2.  
  3. % pdf_2ps.ps
  4. % PDF to PostScript additions to PDF reader.
  5.  
  6. GS_PDF_ProcSet begin
  7. pdfdict begin
  8.  
  9. /.setlanguagelevel where { pop 2 .setlanguagelevel } if
  10. .currentglobal true .setglobal
  11.  
  12. % Generate a sufficiently unique name (at least unique within the current
  13. % save/restore environment).
  14. /uniqueid#
  15.  { userdict length
  16.     { dup neg =string cvs dup 0 (_) putinterval cvn
  17.       userdict 1 index known not { exch pop exit } if pop 1 add
  18.     }
  19.    loop
  20.  } bdef
  21.  
  22. % "Wrap" all the runtime operators so they call #exec.
  23. numargsdict
  24.  { 1 index load exch 2 index exch /#exec cvx 4 packedarray cvx def
  25.  }
  26. forall
  27. /pdfmark
  28.   /pdfmark load dup type /operatortype eq { 1 packedarray cvx } if
  29.    { /pdfmark counttomark 1 sub #exec } bind
  30.   aload length 1 add packedarray cvx
  31. def
  32. % Define #exec so it also prints out its arguments.
  33. /dictwrite#        % <file> <dict> dictwrite# -
  34.  { dup length 240 le
  35.     { 1 index (mark) writestring
  36.        { 2 index ( ) writestring exch 2 index exch write#
  37.      1 index ( ) writestring 1 index exch write#
  38.        }
  39.       forall dup ( .dicttomark) writestring
  40.     }
  41.     { 2 copy length write=only 1 index ( dict) writestring
  42.        { exch 2 index dup ( dup ) writestring exch write#
  43.      1 index dup ( ) writestring exch write#
  44.      dup ( put) writestring
  45.        }
  46.       forall
  47.     }
  48.    ifelse pop
  49.  } bdef
  50. /fontwrite#        % <file> <font> fontwrite# -
  51.  {        % Find the named font and then modify it.
  52.    2 copy /FontName get =string cvs
  53.     { dup dup length 1 sub 1 getinterval (%) ne { exit } if
  54.       0 1 index length 1 sub getinterval
  55.     }
  56.    loop cvn write==only
  57.    1 index ( findfont) writestring
  58.         % Insert the appropriate Encoding, by name if possible.
  59.    dup /Encoding get
  60.    dup dup StandardEncoding eq exch ISOLatin1Encoding eq or
  61.     { 3 index ( dup /Encoding get ) writestring
  62.       StandardEncoding eq { (StandardEncoding) } { (ISOLatin1Encoding) } ifelse
  63.       3 index 1 index writestring
  64.       3 index ( ne { dup length dict copy dup /FID undef dup /Encoding ) writestring
  65.       3 index exch writestring
  66.       2 index ( put /_ exch definefont } if\n) writestring
  67.     }
  68.     { 2 index ( dup length dict copy dup /FID undef dup /Encoding\n) writestring
  69.       2 index exch write#
  70.       1 index (\nput /_ exch definefont\n) writestring
  71.     }
  72.    ifelse
  73.         % Check for modified Metrics.
  74.    dup /Metrics .knownget
  75.     { 2 index ( dup length dict copy\n) writestring
  76.       2 index ( dup /FID undef dup /UniqueID undef dup /Metrics\n) writestring
  77.       2 index exch write#
  78.       1 index (\nput /_ exch definefont\n) writestring
  79.     }
  80.    if pop pop
  81.  } bdef
  82. /write#dict 10 dict dup begin
  83.   /arraytype
  84.    { dup xcheck { (}) ({) } { (]) ([) } ifelse
  85.      2 index length 0 eq
  86.       { 3 index exch writestring exch
  87.       }
  88.       { 3 -1 roll
  89.      { 3 index 2 index writestring 3 index exch write# pop ( ) }
  90.     forall
  91.       }
  92.      ifelse pop writestring
  93.    } bdef
  94.   /dicttype
  95.    { null userdict { 3 index eq { exch pop exit } if pop } forall
  96.      dup null eq
  97.       { pop 2 copy dup /FID known { fontwrite# } { dictwrite# } ifelse
  98.     1 index ( userdict ) writestring
  99.     uniqueid# 2 index 1 index write# 2 index ( 2 index put) writestring
  100.     userdict exch 3 -1 roll put pop
  101.       }
  102.       { exch pop cvx write==only
  103.       }
  104.      ifelse
  105.    } bdef
  106.   /marktype
  107.    { pop ([) writestring
  108.    } bdef
  109. end def
  110. /write#
  111.  { dup type //write#dict exch .knownget { exec } { write==only } ifelse
  112.  } bind def
  113.  
  114. % Rebind the procedures that conditionally write out PostScript.
  115.  
  116.  
  117. /#            % <arg1> ... <argN> <opname> <N> # -
  118.  { 1 index load 3 1 roll #exec
  119.  } bdef
  120. /#?            % - #? <writing>
  121.  { /PSout where { pop true } { false } ifelse
  122.  } bdef
  123. /#exec        % <arg1> ... <argN> <proc|operator> <opname> <N> #exec -
  124.  { /PSout where
  125.     { pop -1 1 { 1 add index PSout exch write# PSout ( ) writestring } for
  126.       PSout exch write=
  127.     }
  128.     { pop pop
  129.     }
  130.    ifelse exec
  131.  } bdef
  132. /#dsc        % mark <obj1> ... #dsc -
  133.  { /PSout where
  134.     { pop counttomark
  135.        { counttomark -1 roll PSout exch write=only }
  136.       repeat pop PSout (\n) writestring
  137.     }
  138.     { cleartomark
  139.     }
  140.    ifelse
  141.  } bdef
  142. /iscomment    % <line> iscomment <line> <bool>
  143.  { dup () eq { true } { dup 0 1 getinterval (%) eq } ifelse
  144.  } bdef
  145. /#dscfile    % <filename> #dscfile -
  146.  { /PSout where
  147.     { pop findlibfile
  148.        { exch pop }
  149.        { (r) file }        % let the error happen
  150.       ifelse
  151.        { dup =string readline pop
  152.      iscomment { mark exch #dsc } { exit } ifelse
  153.        }
  154.       loop mark exch #dsc
  155.        { dup =string readline not { pop exit } if
  156.      iscomment
  157.       { pop }
  158.       { (%) search { exch pop exch pop } if mark exch #dsc }
  159.      ifelse
  160.        }
  161.       loop closefile
  162.     }
  163.     { pop
  164.     }
  165.    ifelse
  166.  } bdef
  167.  
  168. % Rebind Is, which constructs a data source for an image.
  169. % pdf_draw defined it to simply retrieve the stream.
  170. /Is_draw /Is load def
  171. /EI_draw /EI load def
  172. userdict /Is_string null put    % establish a binding
  173. userdict /Is_text null put    % ditto
  174. userdict /Is_data null put    % ditto
  175. /Is        % <imagedict> Is <imagedict> <datasource>
  176.  { /PSout where
  177.     { pop dup /DataSource get string /Is_string exch store
  178.       /Is_text PSout /ASCII85Encode filter store
  179.       /Is_data Is_text Is_string length /RunLengthEncode filter store
  180.       Is_draw
  181.        { Is_string readstring pop Is_data 1 index writestring }
  182.       aload length 1 add packedarray cvx
  183.     }
  184.     { Is_draw
  185.     }
  186.    ifelse
  187.  } bdef
  188. /EI
  189.  { /PSout where { pop Is_data closefile Is_text closefile } { EI_draw } ifelse
  190.  } bdef
  191.  
  192. % Rebind readfontfilter, which constructs the filter that
  193. % reads the text of an embedded Type 1 (and eventually Type 3) font.
  194. /readfontfilter_orig /readfontfilter load def
  195. /readfontfilter        % <proc> readfontfilter <filter>
  196.  { /copyfontdata cvx 2 array astore cvx
  197.    0 () /SubFileDecode filter
  198.  } bdef
  199. /copyfontdata        % <string> <origproc> copyfontdata <substring>
  200.  { exec /PSout where { pop PSout 1 index writestring } if
  201.  } bdef
  202.  
  203. end        % pdfdict
  204.  
  205. .setglobal
  206. end        % GS_PDF_ProcSet
  207.